/**
 *  登录表单控制器
 */
import tem from './index.html';
import $_fn from '~plugins'
class enterCtrl {
  constructor($scope,$rootScope,$timeout,$interval,http) {
    // let $scope = this
      $_fn.changeRouteShowView()
      this.$timeout = $timeout
      let strParam = 'logIncodeImg'
      var token, timeSucc, timeErr
      //定义变量 用于添加class 高亮提醒用户（红色）
      $scope.error = {
          user:false,
          psw:false,
          code:false,
          phone:false,
          phoneCode:false,
          phoneCode1:false,
          resetpsw0:false,
          resetpsw1:false
      }
      //提示信息默认不显示 声明参数
      $scope.tishi = {
          succ:false,
          err:false
      }
      //  登录 声明参数 页面ng-model绑定传送数据到后台
      $scope.UnLogIn = {
          username:'',
          password:'',
          changeCode:'',
          token:''
      }
      //找回密码 页面ng-model绑定传送数据到后台
      $scope.findPsw = {
          phone:'',
          changeCode:'',
          token:''
      }
      //请输入手机验证码
      $scope.enterPsw = {
          phone:'',
          codes:''
      }
      $scope.enterPsw.phone = $rootScope.userPhone
      //重置密码
      $scope.resetPsw = {
          psw0:'',
          psw1:'',
          phone:''
      }
      //ajax回调函数
      function getCode(result){
          //闭包函数 实现局部传参
          return cloCallback(result,strParam)
      }
      // 回调函数的闭包函数
      function cloCallback(result,str){
          $('.' + str).attr('src',result.content.img)
          //赋值token 重新发送给后端验证
          token = result.content.token
      }
      //点击表单是隐藏错误信息提醒
      $scope.hideErr = function(){
          $('.tixingFalse').css({'right':'-250px'})
          $scope.tishi.err = false
          $scope.tishi.succ = false
          for(var item in $scope.error){
              $scope.error[item] = false
          }
      }
      //btn点击获取验证码并写入img中   angular函数
      $scope.getLogInCode = function($event,params){
          if(params == 'logIncodeImg') {
              strParam = params//更改需要更新图片的节点
              http._post('imageCode.do',getCode)
          }
          if(params == 'findPswImg'){
              strParam = params//更改需要更新图片的节点
              http._post('imageCode.do',getCode)
          }
      }
      // 首次加载html发送ajax请求获取验证码
      $scope.defaultAjax = function(){
          http._post('imageCode.do',getCode)
      }
      //登录、找回密码点击页面内跳转跳转   跳转发送ajax请求获取验证码
      $scope.logInLocationPath = function(params){

          // 回传登录成功模板切换
          $scope.$emit('template', params)
          if(params == 'findPsw'){
              strParam = 'findPswImg'//更改需要更新图片的节点
              http._post('imageCode.do',getCode)
          }
          if(params == 'logIn'){
              strParam = 'logIncodeImg'//更改需要更新图片的节点
              http._post('imageCode.do',getCode)
          }
      }

      //btn点击获取验证码并写入img中   angular函数
      $scope.getLogInCode = function($event,params){
          if(params == 'logIncodeImg') {
              strParam = params//更改需要更新图片的节点
              $_fn.ajaxPostImg('imageCode.do','',getCode)
          }
          if(params == 'findPswImg'){
              strParam = params//更改需要更新图片的节点
              $_fn.ajaxPostImg('imagde.do','',getCode)
          }
      }

      // 首次加载html发送ajax请求获取验证码
      $scope.defaultAjax = function(){
          console.log('1')
          $_fn.ajaxPostImg('imageCode.do','',getCode)
      }
      //键盘回车时提交表单
      $scope.key = function(e,param){
          var keycode = window.event?e.keyCode:e.which
          if(keycode == 13){                //回车触发
              if(param == 'logIn'){           //登录界面点击提交触发的方法
                  $scope.logInSuccess('logInSuccess')
              }
              if(param == 'find'){            //找回密码
                  $scope.findPswFun('enterPsw')
              }
              if(param == 'enter'){           //找回密码-输入验证码
                  $scope.enterPswFun()
              }
              if(param == 'reset'){           //找回密码-重置密码
                  $scope.resetPswFun()
              }
          }
      }
    var timer
    //请输入验证码 60s重新获取
    function interval(){
      $interval.cancel(timer)
      timer = $interval(function(){
        var num = $('._span').html()
        num--
        $('._span').html(num)
        if(num == 0){
          $interval.cancel(timer)
          $('.spanParent').css('display','none')
          $('.btnGet').css('display','block')
          $('._span').html('60')
        }
      },1000)
    }

    //请输入验证码 发送ajxa请求 传入
  $scope.enterPswFun = function(){

    if( /^\s*$/.test($scope.enterPsw.codes) ){
      console.log($scope.enterPsw.codes)
      $scope.errorFun('验证码不能为空',1)
      $scope.error.phoneCode1 = true
      return
    }
    $_fn.ajaxPost('checkPhoneCaptcha.do',
      {phone:$scope.enterPsw.phone,captcha:$scope.enterPsw.codes},function(result){
        console.log(result)
        if(result.type == 1){
          $scope.resetPsw.phone = result.content//获取手机号码 存在reset中 重置密码再次传入后端验证
          $scope.successFun(result.msg)
          // $scope.logIn = 'work/tems2/logIn/resetPsw.html'
          $scope.$emit('template', 'resetPsw')
        }else{
          $scope.errorFun(result.msg)
          $scope.error.phoneCode1 = true
        }
      })
  }
  //请输入验证码重新获取手机验证码
  $scope.btnGet = function(){
    $('.spanParent').css('display','block')
    $('.btnGet').css('display','none')
    $_fn.ajaxPost('sendmsg.do',{phone:$scope.enterPsw.phone},function(result){
      console.log(result)
      interval()
    })
  }

  }
}
enterCtrl.$inject = ['$scope','$rootScope','$timeout','$location','$interval','http']
module.exports = angular.module('directive.enterpsw', []).directive('enterPsw', function() {
    return {
        restrict: 'E',
        template: tem,
        replace:true,
        controller: enterCtrl
    }
}).name